from django.contrib.auth.hashers import make_password
from django.shortcuts import render, redirect
from django.views import View

from blog.forms import RegisterForm


# 21-实验6.1 系统注册-用户注册
def register(request):
    if request.method == 'GET':
        return render(request,
                      'register.html')
    elif request.method == 'POST':
        register_forms = RegisterForm(request.POST)
        if register_forms.is_valid():
            # 返回用户模型对象(commit=False意思是暂不提交至数据库)
            user = register_forms.save(commit=False)
            # 激活用户
            user.is_active = 1
            # 生成密码(使用系统内置编码函数)
            user.password = make_password(register_forms.cleaned_data['password'])
            # 保存用户模型对象(提交至数据库)
            user.save()
            return redirect('/blog')
        else:
            return render(request,
                            'register.html',
                                {'forms': register_forms})

# 21-实验6.2 系统注册-用户登录
# class LoginView(View):
#     def get(self, request):
#         pass
#         # TODO
#
#     def post(self, request):
#         pass
#         # TODO
